import { defineStore } from 'pinia'
import {
  reqCategory1List,
  reqCategory2List,
  reqCategory3List,
} from '../../api/product/attr/index'

import type { CategoryResponseData } from '../../api/product/attr/type'

const useCategoryStore = defineStore('Category', {
  state: () => {
    return {
      // 存储一级分类的数据
      c1Arr: [],
      // 存储一级分类的id
      c1Id: '',
      // 存储二级分类的数据
      c2Arr: [],
      // 存储二级分类的id
      c2Id: '',
      // 存储三级分类的数据
      c3Arr: [],
      // 存储三级分类的id
      c3Id: '',
    }
  },

  actions: {
    // 获取一级分类数据
    async getCategory1List() {
      //发请求获取一级分类的数据
      const result = await reqCategory1List()
      if (result.code == 200) {
        this.c1Arr = result.data
      }
    },

    //获取二级分类的数据
    async getC2() {
      //获取对应一级分类的下二级分类的数据
      const result: CategoryResponseData = await reqCategory2List(this.c1Id)
      if (result.code == 200) {
        this.c2Arr = result.data
      }
    },

    //获取三级分类的数据
    async getC3() {
      const result: CategoryResponseData = await reqCategory3List(this.c2Id)
      if (result.code == 200) {
        this.c3Arr = result.data
      }
    },
  },
  getters: {},
})

export default useCategoryStore
